package com.qmds.patrolserver.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmds.common.core.entity.Patrol;
import com.qmds.common.core.page.PageVO;
import com.qmds.common.core.utils.UserUtils;
import com.qmds.common.core.vo.R;
import com.qmds.patrolserver.plan.dao.PatrolDao;
import com.qmds.patrolserver.plan.params.PatrolNum;
import com.qmds.patrolserver.plan.params.PatrolPageParam;
import com.qmds.patrolserver.plan.vo.PatrolVo;
import com.qmds.patrolserver.plan.service.PatrolService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * (Patrol)表服务实现类
 *
 * @author makejava
 * @since 2024-09-09 16:41:12
 */
@Service("patrolService")
public class PatrolServiceImpl implements PatrolService {

    @Resource
    private PatrolDao patrolDao;


    @Override
    public R selectAll(PatrolPageParam partolParam) {
        Integer pageNum = partolParam.getPageNum();
        Integer pageSize = partolParam.getPageSize();
        Page<PatrolVo> page = new Page<>(pageNum,pageSize);

        QueryWrapper<PatrolVo> wrapper = getDevicePageVOQueryWrapper(partolParam);
        page = patrolDao.selectAllInfo(page, wrapper);

        PageVO<PatrolVo> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setCurrentPage(page.getCurrent());
        List<PatrolVo> records = page.getRecords();
        pageVO.setDataList(records);

        return R.ok(page);
    }

    @Override
    public R insertIfo(PatrolPageParam patrolParam) {
        Patrol patrol = new Patrol();
        BeanUtils.copyProperties(patrolParam, patrol);
        patrol.setEnableStatus(1);
        Integer id = UserUtils.getId();
        patrol.setUid(id);
        return R.ok(patrolDao.insert(patrol));
    }

    @Override
    public R typeCount() {
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> numbers = new ArrayList<>();
        List<String> type = new ArrayList<>();
        int sum = 0;
        type.add("全部计划");
        numbers.add(sum);
        for (PatrolNum patrolNum : patrolDao.selectNum()) {
            type.add(patrolNum.getPlanType());
            numbers.add(patrolNum.getPlanNum());
        };
        for (int i = 0; i < type.size(); i++) {
            if (type.get(i).equals("A")){
                type.set(i,"计划类型A");
            }else if (type.get(i).equals("B")){
                type.set(i,"计划类型B");
            }else if (type.get(i).equals("C")){
                type.set(i,"计划类型C");
            }else if (type.get(i).equals("D")){
                type.set(i,"计划类型D");
            }
            sum += numbers.get(i);
        }
        map.put("type", type);
        numbers.set(0,sum);
        map.put("numbers", numbers);
        return R.ok(map);
     }
    private QueryWrapper<PatrolVo> getDevicePageVOQueryWrapper(PatrolPageParam partolParam) {
        QueryWrapper<PatrolVo> wrapper = new QueryWrapper<>();

        if (partolParam.getPlanName() != null && partolParam.getPlanName() != "") {
            wrapper.like("p.plan_name", partolParam.getPlanName());
        }
        if (partolParam.getPatrol() != null) {
            wrapper.like("p.patrol", partolParam.getPatrol());
        }

        if (partolParam.getUsername() != null && partolParam.getUsername() != "") {
            wrapper.like("u.username", partolParam.getUsername());
        }
        if (partolParam.getPlanName() != null && partolParam.getPlanName() != "") {
            wrapper.like("p.plan_name", partolParam.getPlanName());
        }
        if (partolParam.getCreateDate() != null ) {
            wrapper.like("p.create_date", partolParam.getPlanName());
        }
        if (partolParam.getPhone() != null && partolParam.getPhone() != "" ) {
            wrapper.like("u.phone", partolParam.getPlanName());
        }
        if (partolParam.getEnableStatus() != null) {
            wrapper.like("p.enable_status", partolParam.getEnableStatus());
        }
        if (partolParam.getPlanType() !='\0') {
            wrapper.like("p.plan_type", partolParam.getPlanType());
        }


        return wrapper;
    }
}

